/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NhapDiemHTFrame.java
 *
 * Created on Jul 27, 2010, 11:02:50 AM
 */
package GUI;

import BUS.DiemHocTapBUS;
import BUS.HocSinhBUS;
import BUS.LopBUS;
import BUS.MonHocBUS;
import BUS.NamHocBUS;
import POJO.DiemHocTapPOJO;
import POJO.HocSinhPOJO;
import POJO.LopPOJO;
import POJO.MonHocPOJO;
import POJO.NamHocPOJO;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author MinhHieu
 */
public class NhapDiemHTFrame extends javax.swing.JInternalFrame {

    private ArrayList<String> DSMaLop = new ArrayList<String>();

    public void loadjLNamHoc() {
        ArrayList<NamHocPOJO> NamHocList = NamHocBUS.selectAll();
        int num = NamHocList.size();
        if (num == 0) {
            this.jLNamHoc.setText("Danh sách năm học trống.");
        } else {
            this.jLNamHoc.setText(NamHocList.get(num - 1).getNamHoc());
        }
    }

    public void loadjCBLop() {
        this.DSMaLop.clear();
        DefaultComboBoxModel dcm = new DefaultComboBoxModel();
        ArrayList<LopPOJO> LopList = LopBUS.selectLop_Nam(this.jLNamHoc.getText());
        for (int i = 0; i < LopList.size(); i++) {
            this.DSMaLop.add(LopList.get(i).getMaLop());
            dcm.addElement(LopList.get(i).getMaLop());
        }
        this.jCBLop.setModel(dcm);

    }

    public void loadjCBMon() {

        DefaultComboBoxModel dcm = new DefaultComboBoxModel();
        ArrayList<MonHocPOJO> MonList = MonHocBUS.selectAll();
        for (int i = 0; i < MonList.size(); i++) {
            dcm.addElement(MonList.get(i).getTenMonHoc());
        }
        this.jCBMonHoc.setModel(dcm);

    }

    public void loadbang() {
        try {
            String malop = this.DSMaLop.get(this.jCBLop.getSelectedIndex());
            ArrayList<HocSinhPOJO> HSList = HocSinhBUS.selectHocSinh_Lop(malop);
            DefaultTableModel dtm = new DefaultTableModel();
//            {
//                @Override
//                public Class getColumnClass(int col){
//                    if(col == 3 || col == 4 || col == 5){
//                        return Double.class;
//                    }else{
//                        return Object.class;
//                    }
//                }
//            };
            dtm.addColumn("STT");
            dtm.addColumn("Mã học sinh");
            dtm.addColumn("Họ và tên");
            dtm.addColumn("Điểm 15 phút");
            dtm.addColumn("Điểm 1 tiết");
            dtm.addColumn("Điểm cuối kì ");
            dtm.setRowCount(HSList.size());
            for (int i = 0; i < HSList.size(); i++) {
                ArrayList<NamHocPOJO> NamHocList = NamHocBUS.selectAll();
                int num = NamHocList.size();
                String MaNam = NamHocList.get(num - 1).getMaNamHoc();
                String tenMH = this.jCBMonHoc.getSelectedItem().toString();
                String mamh = MonHocBUS.laymamhtheotenmh(tenMH);
                dtm.setValueAt(i + 1, i, 0);
                dtm.setValueAt(HSList.get(i).getMaHS(), i, 1);
                dtm.setValueAt(HSList.get(i).getTenHS(), i, 2);
                ArrayList<DiemHocTapPOJO> d = DiemHocTapBUS.loaddulieudiembenNhapDiem(HSList.get(i).getMaHS(), mamh, this.jCBHocKi.getSelectedItem().toString(), MaNam);
                if (d.size() > 0) {
                    dtm.setValueAt(d.get(0).getDiem15(), i, 3);
                    dtm.setValueAt(d.get(0).getDiem1Tiet(), i, 4);
                    dtm.setValueAt(d.get(0).getDiemCuoiKy(), i, 5);
                }
            }

            this.jTDiem.setModel(dtm);
            this.jTDiem.changeSelection(0, 0, false, false);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /** Creates new form NhapDiemHTFrame */
    public NhapDiemHTFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jCBLop = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jCBMonHoc = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLNamHoc = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jCBHocKi = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDiem = new javax.swing.JTable();
        jBThoat = new javax.swing.JButton();
        jBLuu = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setClosable(true);
        setForeground(java.awt.Color.white);
        setIconifiable(true);
        setTitle("Quản lý học sinh - Nhập điểm môn học");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel1.setForeground(new java.awt.Color(0, 102, 204));
        jLabel1.setText("NHẬP ĐIỂM");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 255))); // NOI18N

        jLabel2.setText("Lớp: ");

        jCBLop.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBLopItemStateChanged(evt);
            }
        });

        jLabel3.setText("Môn học:");

        jCBMonHoc.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBMonHocItemStateChanged(evt);
            }
        });

        jLabel4.setText("Năm học:");

        jLNamHoc.setText("loading");

        jLabel5.setText("Học Kì: ");

        jCBHocKi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2" }));
        jCBHocKi.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBHocKiItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jLNamHoc))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jCBMonHoc, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(67, 67, 67)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCBHocKi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBLop, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(64, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLNamHoc)
                    .addComponent(jLabel2)
                    .addComponent(jCBLop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jCBMonHoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jCBHocKi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Bảng Điểm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        jTDiem.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTDiem);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addContainerGap())
        );

        jBThoat.setText("Thoát");
        jBThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBThoatActionPerformed(evt);
            }
        });

        jBLuu.setText("Lưu");
        jBLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBLuuActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel6.setText("*Lưu ý: Sau khi nhập xong nhấn enter");
        jLabel6.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(206, 206, 206)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(221, 221, 221))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jBThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jBLuu, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)))
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(333, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(51, 51, 51))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jBLuu)
                        .addGap(28, 28, 28)
                        .addComponent(jBThoat)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThoatActionPerformed
        // TODO add your handling code here:
        int xacnhan = JOptionPane.showConfirmDialog(this, "Bạn thực sự muốn thoát", "Xác nhận", JOptionPane.YES_NO_OPTION);
        if(xacnhan==0){
            this.dispose();
        }
    }//GEN-LAST:event_jBThoatActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.loadjLNamHoc();
        this.loadjCBLop();
        this.loadjCBMon();
        this.loadbang();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jBLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBLuuActionPerformed
        // TODO add your handling code here:
        ArrayList<NamHocPOJO> NamHocList = NamHocBUS.selectAll();
        int num = NamHocList.size();
        String MaNam = NamHocList.get(num - 1).getMaNamHoc();
        String hki = this.jCBHocKi.getSelectedItem().toString();
        String tenMH = this.jCBMonHoc.getSelectedItem().toString();
        String mamh = MonHocBUS.laymamhtheotenmh(tenMH);
        ///////////////////////////////////////////////////
        int n = this.jTDiem.getRowCount();
        for (int i = 0; i < n; i++) {
            String diem15,diem1tiet,diemcki;
            if(this.jTDiem.getValueAt(i, 3) == null || this.jTDiem.getValueAt(i, 3).toString().equals("")){
                diem15="NULL";
            }else{
                diem15 = (this.jTDiem.getValueAt(i, 3).toString());
            }
            if(this.jTDiem.getValueAt(i, 4) == null || this.jTDiem.getValueAt(i, 4).toString().equals("")){
                diem1tiet="NULL";
            }else{
                diem1tiet = (this.jTDiem.getValueAt(i, 4).toString());
            }
            if(this.jTDiem.getValueAt(i, 5) == null || this.jTDiem.getValueAt(i, 5).toString().equals("")){
                diemcki="NULL";
            }else{
                diemcki = (this.jTDiem.getValueAt(i, 5).toString());
            }
            String mahs = (String) this.jTDiem.getValueAt(i, 1);

            ///////////////////////////////////////////////
            ArrayList<DiemHocTapPOJO> d = DiemHocTapBUS.loaddulieudiembenNhapDiem(mahs, mamh, hki, MaNam);
            if (d.size() > 0) {
                String diemtest1 = d.get(0).getDiem15();
                String diemtest2 = d.get(0).getDiem1Tiet();
                String diemtest3 = d.get(0).getDiemCuoiKy();
                if (diem15.equals(diemtest1)==false || diem1tiet.equals(diemtest2)==false || diemcki.equals(diemtest3)==false) {
                    boolean kq = DiemHocTapBUS.suaDiem(mahs, mamh, hki, MaNam, diem15, diem1tiet, diemcki);
                     if (kq) {
                                JOptionPane.showMessageDialog(this, "Sửa thành công.");
                    } else {
                                JOptionPane.showMessageDialog(this, "Sửa thất bại");
                    }
                }

            } else {
                if(diem15.compareTo("NULL")!=0 || diem1tiet.compareTo("NULL")!=0 || diemcki.compareTo("NULL")!=0 ){
                    boolean kq = DiemHocTapBUS.nhapDiemNhapDiem(mahs, mamh, hki, MaNam, diem15, diem1tiet, diemcki);
                    if (kq) {
                                JOptionPane.showMessageDialog(this, "Nhập thành công.");
                    } else {
                                JOptionPane.showMessageDialog(this, "Nhập thất bại");
                    }
                }
            }
          
        }
//       
        this.loadbang();

    }//GEN-LAST:event_jBLuuActionPerformed

    private void jCBMonHocItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBMonHocItemStateChanged
        // TODO add your handling code here:
        this.loadbang();

    }//GEN-LAST:event_jCBMonHocItemStateChanged

    private void jCBLopItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBLopItemStateChanged
        // TODO add your handling code here:
        this.loadbang();
    }//GEN-LAST:event_jCBLopItemStateChanged

    private void jCBHocKiItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBHocKiItemStateChanged
        // TODO add your handling code here:
        this.loadbang();
    }//GEN-LAST:event_jCBHocKiItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBLuu;
    private javax.swing.JButton jBThoat;
    private javax.swing.JComboBox jCBHocKi;
    private javax.swing.JComboBox jCBLop;
    private javax.swing.JComboBox jCBMonHoc;
    private javax.swing.JLabel jLNamHoc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTDiem;
    // End of variables declaration//GEN-END:variables
}
